package com.example.eback.service;

import com.example.eback.dao.FilesDAO;
import com.example.eback.dao.OrganInfoDAO;
import com.example.eback.entity.OrganInfo;
import com.example.eback.entity.UpLoadFile;
import com.example.eback.result.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;
import java.util.List;

@Service
public class AnalyzeService {
    @Autowired
    private FilesDAO filesDAO;
    @Autowired
    private FileProcessService fileProcessService;
    @Autowired
    private OrganInfoDAO organInfoDAO;
    public void Measurement(int current_userID, int historyid){
        UpLoadFile currentFile=filesDAO.findByUserIdAndId(current_userID,historyid);
        int modelname=currentFile.getModelName();
        if(modelname==1){
            MeasurementData measurementData=fileProcessService.Measurement(currentFile.getVnetoriginfileUrl(), currentFile.getLabelUrl());
            // 从 MeasurementData 对象中获取 measurements
            Map<String, OrganData> measurements = measurementData.getMeasurements();
            for (Map.Entry<String, OrganData> entry : measurements.entrySet()) {
                // 获取器官名和对应的测量数据
                String organName = entry.getKey();
                OrganData organData = entry.getValue();

                // 创建一个新的 OrganInfo 对象
                OrganInfo organInfo = new OrganInfo();

                // 设置 OrganInfo 的各种属性
                // 注意：此处假设 organId 已经有其他方式生成或获取，这里用 0 代替
                organInfo.setOrganName(organName);
                organInfo.setHistoryId(historyid);  // 假设所有器官共享同一 URL
                organInfo.setDiameterX(organData.getDiameterX());
                organInfo.setDiameterY(organData.getDiameterY());
                organInfo.setDiameterZ(organData.getDiameterZ());
                organInfo.setVolume(organData.getVolume());
                organInfo.setArea(organData.getArea());
                organInfo.setFlatness(organData.getFlatness());
                organInfo.setElongation(organData.getElongation());
                // 保存 OrganInfo 对象到数据库
                organInfoDAO.save(organInfo);
            }
        }
        if(modelname==2||modelname==3){
            MeasurementData measurementData=fileProcessService.Measurement(currentFile.getVnetoriginfileUrl(), currentFile.getLabelUrl());
            // 从 MeasurementData 对象中获取 measurements
            Map<String, OrganData> measurements = measurementData.getMeasurements();
            for (Map.Entry<String, OrganData> entry : measurements.entrySet()) {
                // 获取器官名和对应的测量数据
                String organName = entry.getKey();
                OrganData organData = entry.getValue();

                // 创建一个新的 OrganInfo 对象
                OrganInfo organInfo = new OrganInfo();

                // 设置 OrganInfo 的各种属性
                // 注意：此处假设 organId 已经有其他方式生成或获取，这里用 0 代替
                organInfo.setOrganName(organName);
                organInfo.setHistoryId(historyid);  // 假设所有器官共享同一 URL
                organInfo.setDiameterX(organData.getDiameterX());
                organInfo.setDiameterY(organData.getDiameterY());
                organInfo.setDiameterZ(organData.getDiameterZ());
                organInfo.setVolume(organData.getVolume());
                organInfo.setArea(organData.getArea());
                organInfo.setFlatness(organData.getFlatness());
                organInfo.setElongation(organData.getElongation());
                // 保存 OrganInfo 对象到数据库
                organInfoDAO.save(organInfo);
            }
        }
    }
    public List<OrganInfo> getOrganInfo(int historyid){
        return organInfoDAO.findAllByHistoryId(historyid);

    }
    public double CalculateDistance(int uid,int historyid,int label_one,int label_two){
        UpLoadFile currentFile=filesDAO.findByUserIdAndId(uid,historyid);
        int modelname=currentFile.getModelName();
        if(modelname==1){
            double distance=fileProcessService.CalculateDistance(currentFile.getVnetoriginfileUrl(), currentFile.getLabelUrl(),
                    label_one,label_two);
            return distance;
        }
        if(modelname==2||modelname==3){
            double distance=fileProcessService.CalculateDistance(currentFile.getVnetoriginfileUrl(), currentFile.getLabelUrl(),
                    label_one,label_two);
            return distance;
        }
        else{
            return 0;
        }

    }
}
